package com.webmall.servlet.user;

/**
 * 用戶注冊的處理 與添加用戶實現一樣
 */

import com.webmall.entity.UserEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UserRegiter")
public class UserRegiter extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //根据add界面的name参数进行内容获取
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");

        //创建用户书体
        //默认添加用户身份为1 普通用户
        UserEntity user = new UserEntity
                (id,name,pwd,sex,birthday,null,email,mobile,address,1);


        //加入数据库的表中
        int count=com.webmall.service.webmall_UserDao.insertUser(user);


        //成功或失败重定位到哪儿
        if (count >0){
            response.sendRedirect("login.jsp");//成功
//            PrintWriter out = response.getWriter();
//            out.write("<script>");
//            out.write("alert('用户添加成功')");
//            out.write("location.href='manageHtml/admin_useradd.jsp'");
//            out.write("</script>");

        }else {//失败 用输出流输出 js语句
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('用户添加失败')");
            out.write("location.href='reg.jsp'");
            out.write("</script>");

        }
    }


}
